package file.day9_25.day09;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class Server2 {
    private ServerSocket serverSocket;
    private Socket socket;
//    private PrintWriter[] allOut = {};
    private Collection<PrintWriter> allOut = new ArrayList<>();
    private String host;

    public Server2(){
        System.out.println("正在启动端口8094服务器");

        try {
            serverSocket = new ServerSocket(8094);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("启动服务器完成");

    }

    private void start(){
        try {

            while (true){
                System.out.println("正在连接服务器");
                socket = serverSocket.accept();
                System.out.println("连接成功");

                host = socket.getInetAddress().getHostAddress();

                SocketHandler socketHandler = new SocketHandler();
                Thread t = new Thread(socketHandler);
                t.start();
            }





        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sendMessage(String message){
        synchronized(allOut){
//            for (int i = 0; i < allOut.length; i++) {
//                allOut[i].println(message);
//            }
            for (PrintWriter pw : allOut){
                pw.println(message);
            }
        }

    }

    private class SocketHandler implements Runnable{
        public void run(){
            BufferedReader br = null;
            PrintWriter pw = null;

            try {
                br = new BufferedReader(
                        new InputStreamReader(
                                socket.getInputStream(),"UTF-8"
                        )
                );

                pw = new PrintWriter(
                        new BufferedWriter(
                                new OutputStreamWriter(
                                        socket.getOutputStream(),"UTF-8"
                                )
                        ),true
                );

                synchronized (allOut){
//                    allOut = Arrays.copyOf(allOut,allOut.length+1);
//                    allOut[allOut.length-1] = pw;
                    allOut.add(pw);
                }
                String sign = host + "上线了，当前在线人数" + allOut.size();
                sendMessage(sign);
                System.out.println(sign);


                String message = null;
                while ((message = br.readLine())!= null){
                    sendMessage(host + "说：" + message);
                }
            } catch (IOException e) {
                System.out.println("客户端断开异常");
            }finally {
                synchronized (allOut){
                    allOut.remove(pw);
                    String sign = host + "下线了，在线人数" + allOut.size();
                    System.out.println(sign);
                    sendMessage(sign);
                }

            }

        }
    }

    public static void main(String[] args) {
        Server2 server = new Server2();
        server.start();
    }
}
